﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_ReportManage_frm_ReportManage : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindReportInfo(dst, intPageIndex);
        }
    }

    public void AddReportInfo(object sender, EventArgs e)
    {
        string strName = this.txt1.Text.Trim();
        string strUrl = this.txt2.Text.Trim();
        string strRemark = this.txt4.Text.Trim();
        int intStop = Int32.Parse(this.drop3.SelectedItem.Value.Trim());

        if (strName != "")
        {
            ClsReportDAL cls_report = new ClsReportDAL();

            Boolean is_succeed = cls_report.AddReportInfo(strName, strUrl, intStop, strRemark);

            if (is_succeed)
            {
                this.txt1.Text = this.txt2.Text = this.txt4.Text = "";
                this.lblJs.Text = "<script>document.getElementById('" + this.txt1.ClientID + "').focus();</script>";

                this.GetReportDataSet();
            }
            else
            {
                this.lblJs.Text = "<script>alert('新增记录失败，请重试！');</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('报表名称不能为空！');</script>";
        }
    }

    public void UpDateReportInfo(object sender, EventArgs e)
    {
        ImageButton ibtn = (ImageButton)sender;

        int Command = Int32.Parse(ibtn.CommandName);
        
        if (Command == 1)
        {
            this.GetReportDataSet();
        }
        else
        { 
            int Index = Int32.Parse(ibtn.CommandArgument);

            CheckBox chk = (CheckBox)this.dgrdResult.Items[Index].FindControl("chkSelect");
            ImageButton ibtnEdit = (ImageButton)this.dgrdResult.Items[Index].FindControl("ibtnEdit");
            ImageButton ibtnCan = (ImageButton)this.dgrdResult.Items[Index].FindControl("ibtnCan");
            ImageButton ibtnSer = (ImageButton)this.dgrdResult.Items[Index].FindControl("ibtnSer");
            DropDownList drop = (DropDownList)this.dgrdResult.Items[Index].FindControl("dropRs3");
            TextBox txtRs1 = (TextBox)this.dgrdResult.Items[Index].FindControl("txtRs1");
            TextBox txtRs2 = (TextBox)this.dgrdResult.Items[Index].FindControl("txtRs2");
            TextBox txtRs3 = (TextBox)this.dgrdResult.Items[Index].FindControl("txtRs3");
            TextBox txtRs4 = (TextBox)this.dgrdResult.Items[Index].FindControl("txtRs4");

            if (Command == 0)
            {
                chk.Visible = ibtnEdit.Visible = txtRs3.Visible = false;
                ibtnCan.Visible = ibtnSer.Visible = drop.Visible = true;

                txtRs1.ReadOnly = txtRs2.ReadOnly = txtRs4.ReadOnly = false;
                txtRs1.BorderWidth = txtRs2.BorderWidth = txtRs4.BorderWidth = 1;

                this.lblJs.Text = "<script>document.getElementById('" + txtRs1.ClientID + "').focus();</script>";
            }
            else
            {
                if (txtRs1.Text.Trim() != "")
                {
                    int intID = Int32.Parse(this.dgrdResult.Items[Index].Cells[1].Text.Trim());
                    string strName = txtRs1.Text.Trim();
                    string strUrl = txtRs2.Text.Trim();
                    string strRemark = txtRs4.Text.Trim();
                    int intStop = Int32.Parse(drop.SelectedItem.Value.Trim());

                    ClsReportDAL cls_report = new ClsReportDAL();

                    Boolean is_succeed = cls_report.UpdateReportInfo(intID, strName, strUrl, intStop, strRemark);

                    if (is_succeed)
                    {
                        this.GetReportDataSet();
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('修改记录失败，请重试！');</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('报表名称不能为空！');</script>";
                }
            }
        }
    }

    private void DeleteMenuItem(int Command)
    {
        ClsReportDAL cls_report = new ClsReportDAL();

        int intCount = this.dgrdResult.Items.Count;

        for (int i = 0; i < intCount; i++)
        {
            CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

            if (chkBox.Checked)
            {
                int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());

                Boolean bolIsCussee = cls_report.DeleteReportInfo(intID, Command);

                if (!bolIsCussee)
                {
                    this.lblJs.Text = "<script>alert('作废信息失败，请重试！')</script>";
                }
            }
        }

        this.GetReportDataSet();//重新绑定
    }

    private void GetReportSetInfo()
    { 
        ClsReportDAL cls_report = new ClsReportDAL();
        DataSet dst = cls_report.GetReportSetInfo();

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.txtAddr.Text = dst.Tables[0].Rows[0][1].ToString().Trim();//应用服务路径
            this.txtWidht.Text = dst.Tables[0].Rows[0][2].ToString().Trim();//报表宽度
            this.txtHeight.Text = dst.Tables[0].Rows[0][3].ToString().Trim();//报表高度
        }
        else
        {
            this.lblJs.Text = "<script>alert('获取报表服务配置信息失败！');</script>";
        }
    }

    private void GetReportDataSet()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsReportDAL cls_report = new ClsReportDAL();

            DataSet dst = cls_report.GetReportInfo();

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindReportInfo(dst, 0);
            }
            else
            {
                this.dgrdResult.CurrentPageIndex = 0;
                this.dgrdResult.DataSource = null;
                this.dgrdResult.DataBind();
                this.lblRsCounts.Text = "共0条记录";
                this.lblPageCounts.Text = "共0页";
                this.lblCurrPage.Text = "当前第0页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    private void BindReportInfo(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(78, 78, 78);

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetReportSetInfo();
                this.GetReportDataSet();
            }
            else
            {
                this.lblJs.Text = "";
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
            this.txtWidht.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            this.txtHeight.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindReportInfo(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        this.GetReportDataSet();
    }

    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        this.panAddRs.Visible = true;
        this.lblJs.Text = "<script>document.getElementById('" + this.txt1.ClientID + "').focus();</script>";
    }

    protected void imgCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.panAddRs.Visible = false;
    }

    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        int CommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteMenuItem(CommandName);
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int CommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteMenuItem(CommandName);
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool is_stop = bool.Parse(e.Item.Cells[2].Text.Trim());

            TextBox txtRs3 = (TextBox)e.Item.FindControl("txtRs3");
            TextBox remark = (TextBox)e.Item.FindControl("txtRs4");
            ImageButton ibtnSer = (ImageButton)e.Item.FindControl("ibtnSer");
            ImageButton ibtnEdit = (ImageButton)e.Item.FindControl("ibtnEdit");
            DropDownList drop = (DropDownList)e.Item.FindControl("dropRs3");

            drop.SelectedIndex = (is_stop) ? 1 : 0;
            txtRs3.Text = drop.SelectedItem.Text.Trim();

            //修改按回车提交
            if (remark.ReadOnly)
            {
                remark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnSer.ClientID + "').click();}");
            }

            //编辑权限
            string strButtonEdit = this.hidButtonEdit.Value;

            if (strButtonEdit.Trim() == "1")
            {
                ibtnEdit.Visible = true;
            }
        }
    }

    protected void chkEdit_CheckedChanged(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
        {
            this.txtAddr.Enabled = this.txtWidht.Enabled = this.txtHeight.Enabled = this.btnServer.Enabled = this.btnResult.Enabled = true;
            this.txtAddr.BorderColor = this.txtWidht.BorderColor = this.txtHeight.BorderColor = Color.FromArgb(127, 157, 185);
            this.lblJs.Text = "<script>document.getElementById('" + this.txtAddr.ClientID + "').focus();document.getElementById('" + this.txtAddr.ClientID + "').select();</script>";
        }
        else
        {
            this.txtAddr.Enabled = this.txtWidht.Enabled = this.txtHeight.Enabled = this.btnServer.Enabled = this.btnResult.Enabled = false;
            this.txtAddr.BorderColor = this.txtWidht.BorderColor = this.txtHeight.BorderColor = Color.FromArgb(172, 168, 153);
        }
    }

    protected void btnServer_Click(object sender, EventArgs e)
    {
        string strPath = this.txtAddr.Text.Trim();

        if (strPath != "")
        {
            string strWidth = this.txtWidht.Text.Trim();

            if (strWidth != "")
            {
                string strHeight = this.txtHeight.Text.Trim();

                if (strHeight != "")
                {
                    ClsReportDAL cls_report = new ClsReportDAL();

                    Boolean is_succeec = cls_report.UpdateReportSetInfo(strPath, Int32.Parse(strWidth), Int32.Parse(strHeight));

                    if (is_succeec)
                    {
                        this.chkEdit.Checked = false;
                        this.txtAddr.Enabled = this.txtWidht.Enabled = this.txtHeight.Enabled = this.btnServer.Enabled = this.btnResult.Enabled = false;
                        this.txtAddr.BorderColor = this.txtWidht.BorderColor = this.txtHeight.BorderColor = Color.FromArgb(172, 168, 153);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('修改报表服务配置信息失败，请重试！');</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('报表高度不能为空！');</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('报表宽度不能为空！');</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('服务器地址不能为空！');</script>";
        }
    }

    protected void btnResult_Click(object sender, EventArgs e)
    {
        this.GetReportSetInfo();
        this.chkEdit.Checked = false;
        this.txtAddr.Enabled = this.txtWidht.Enabled = this.txtHeight.Enabled = this.btnServer.Enabled = this.btnResult.Enabled = false;
        this.txtAddr.BorderColor = this.txtWidht.BorderColor = this.txtHeight.BorderColor = Color.FromArgb(172, 168, 153);
    }
}
